/*
 * Copyright 2018-10-6 18:56:37 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.core.exception.ManagerException;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.erp.ao.EmployeeAO;
import com.lap.scp.erp.core.EmployeeManager;
import com.lap.scp.erp.domain.EmployeeDO;
import com.lap.scp.erp.query.EmployeeQueryVO;

/**
 * <pre>
 *   默认 员工信息，记录员工基本信息，主要是计算统计金额使用，暂时没有什么特别需要应用实现类，提供员工信息，记录员工基本信息，主要是计算统计金额使用，暂时没有什么特别需要相关的流程应用操作、查询等。
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-6 18:56:37
 * @since 2.0.0
 */
@Service("employeeAO")
public class DefaultEmployeeAO extends AbstractAO implements EmployeeAO {
	/**
	 * 员工信息，记录员工基本信息，主要是计算统计金额使用，暂时没有什么特别需要业务操作对象
	 */
	@Resource
	private EmployeeManager employeeManager;

	@Override
	public Resp<EmployeeDO> insertEmployee(EmployeeDO employeeDO, BindingResult errors) {
		Resp<EmployeeDO> resp = new Resp<>();
		try {
			super.validate(resp, errors, employeeDO);
			this.validateAdd(resp, employeeDO);
			if (resp.isSuccess()) {
				employeeManager.insertEmployee(employeeDO);
				resp.setData(employeeDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(employeeDO, e);
		}
		return resp;
	}

	private void validateAdd(Resp<?> resp, EmployeeDO employeeDO) throws ManagerException {
		EmployeeDO query = employeeManager.queryByName(employeeDO.getFullName());
		if (query != null) {
			resp.putMsg(getMsg("601"));
		}
	}

	@Override
	public Resp<Integer> deleteById(Integer employeeId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = employeeManager.deleteById(employeeId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(employeeId, e);
		}
		return resp;
	}

	@Override
	public Resp<EmployeeDO> queryById(Integer employeeId) {
		Resp<EmployeeDO> model = new Resp<>();
		try {
			EmployeeDO employeeDO = employeeManager.queryById(employeeId);
			model.setData(employeeDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(employeeId, e);
		}
		return model;
	}

	@Override
	public PageResult<EmployeeDO> queryPage(Page page, EmployeeQueryVO queryVO) {
		PageResult<EmployeeDO> pr = new PageResult<>();
		try {
			List<EmployeeDO> list = employeeManager.queryPage(page, queryVO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(new Object[] { page, queryVO }, e);
		}
		return pr;
	}

	@Override
	public Resp<List<EmployeeDO>> queryList() {
		Resp<List<EmployeeDO>> resp = new Resp<>();
		try {
			List<EmployeeDO> list = employeeManager.queryList();
			resp.setData(list);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> updateEmployee(EmployeeDO employeeDO, BindingResult errors) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, errors, employeeDO);
			validateUpdate(resp, employeeDO);
			if (resp.isSuccess()) {
				int flag = employeeManager.updateEmployee(employeeDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(employeeDO, e);
		}
		return resp;
	}

	private void validateUpdate(Resp<?> resp, EmployeeDO employeeDO) throws ManagerException {
		EmployeeDO query = employeeManager.queryByName(employeeDO.getFullName());
		if (query != null && !employeeDO.getId().equals(query.getId())) {
			resp.putMsg(getMsg("601"));
		}
	}

}
